﻿@using WebUI.Areas.Admin.Models
@model UserInfoForEditViewModel
@{
	Layout = "~/Areas/Admin/Views/Shared/_AdminContent.cshtml";

	Html.EnableClientValidation(false);
}
@section head {
	<link href="~/dist/AdminLTE/bower_components/select2/dist/css/select2.min.css" rel="stylesheet" />
	<!-- 重新覆盖掉 select2 所覆盖的主题样式 -->
	<link rel="stylesheet" href="/dist/AdminLTE/dist/css/AdminLTE.min.css">
}
@section body {
	<script src="~/dist/AdminLTE/bower_components/select2/dist/js/select2.full.min.js"></script>
	<script>

		function showMessage(message, type) {
			$("#js-message").fadeIn();
			switch (type) {
				case -1:
					$("#js-message").attr("class", "alert alert-danger").find(".message").html(message);
					break;
				case 1:
					$("#js-message").attr("class", "alert alert-success").find(".message").html(message);
					break;
			}
			// 3s 后隐藏
			setTimeout(function () {
				$("#js-message").fadeOut("slow");
			}, 3000);
		}

		function onSuccess(data) {
			if (data.code <= 0) {
				showMessage(data.message, -1);
			} else {
				showMessage(data.message, 1);
			}
			$("#js-btn-save").html("保存").removeAttr("disabled");
		}

		function saveData() {
			$("#js-btn-save").html("保存中").attr("disabled", "disabled");
			selectRole();
			$.ajax({
				url: "/Admin/UserInfo/Create",
				type: "post",
				dataType: "json",
				data: $("#js-form").serialize(),
				success: onSuccess
			});
			return false;
		}

		function selectRole() {
			var $selectedOptions = $("#js-roleOptions").find("option:selected");
			console.log($selectedOptions);
			var inputHtmlStr = '';
			for (var i = 0; i < $selectedOptions.length; i++) {
				inputHtmlStr += '<input type="hidden" name="RoleOptions[' + i + '].ID" value="' + $selectedOptions[i].value + '" />';
			}
			$("#js-selectedRoleInput").html(inputHtmlStr);
		}

		$(function () {
			//Initialize Select2 Elements
			$(".select2").select2();
			$("#js-roleOptions").change(selectRole);
		});

	</script>
}

<!-- start 提示框 -->
@Html.Partial("_MessagePartial")
<!-- end 提示框 -->
<!-- general form elements -->
<div class="box box-primary">
	<div class="box-header with-border">
		<h3 class="box-title">创建</h3>
	</div>
	<!-- /.box-header -->
	<!-- form start -->
	<form id="js-form">
		@Html.HiddenFor(m => m.ID)
		<div class="box-body">
			<div class="form-group">
				<label for="InputUserName">用户名</label>
				@Html.TextBoxFor(m => m.InputUserName, new { @class = "form-control" })
			</div>
			<div class="form-group">
				<label for="InputName">展示名</label>
				@Html.TextBoxFor(m => m.InputName, new { @class = "form-control", placeholder = "请输入展示名" })
			</div>
			<div class="form-group">
				<label for="InputEmail">邮箱</label>
				@Html.TextBoxFor(m => m.InputEmail, new { @class = "form-control", placeholder = "请输入邮箱", type = "email" })
			</div>
			<div class="form-group">
				<label for="InputEmail">绑定学生-学号</label>
				@Html.TextBoxFor(m => m.InputStudentCode, new { @class = "form-control", placeholder = "请输入绑定学生-学号", type = "text" })
			</div>
			<div class="form-group">
				<label for="InputEmail">绑定员工 - 工号</label>
				@Html.TextBoxFor(m => m.InputEmployeeCode, new { @class = "form-control", placeholder = "请输入绑定员工 - 工号", type = "text" })
			</div>
			<div class="form-group">
				<label for="InputPassword">密码</label>
				@Html.TextBoxFor(m => m.InputPassword, new { @class = "form-control", placeholder = "请输入密码", type = "password" })
			</div>
			@*<div class="form-group">
					<label for="exampleInputFile">头像上传</label>
					<input type="file" id="exampleInputFile">

					<p class="help-block">Example block-level help text here.</p>
				</div>*@
			<div class="form-group">
				<label>角色</label>
				<select id="js-roleOptions" class="form-control select2" multiple data-placeholder="选择角色" style="width: 100%;">
					@foreach (OptionModel option in Model.RoleOptions)
					{
						string selectedStr = option.IsSelected ? "selected" : "";
						<option value="@option.ID" @selectedStr>@option.Text</option>
					}
				</select>
			</div>
			<div id="js-selectedRoleInput" class="form-group" style="display: none;">
			</div>
		</div>
		<!-- /.box-body -->

		<div class="box-footer">
			<div class="btn-group">
				@if (TempData["RedirectUrl"] != null)
				{
					<a class="btn btn-default" href="@TempData["RedirectUrl"].ToString()">返回</a>
				}
				else
				{
					<a class="btn btn-default" href="javascript:history.go(-1);">返回</a>
				}
				<input id="js-btn-save" class="btn btn-primary" type="submit" value="保存" onclick="return saveData();" />
			</div>
		</div>
	</form>
</div>
<!-- /.box -->
